//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using Microsoft.WindowsMobile.Samples.Location;

namespace GpsTest
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class frmMain : System.Windows.Forms.Form
    {
        private System.Windows.Forms.MenuItem exitMenuItem;
        private System.Windows.Forms.MainMenu mainMenu;
        private System.Windows.Forms.Label status;
        private MenuItem menuItemGPS;
        private MenuItem startGpsMenuItem;
        private MenuItem stopGpsMenuItem;


        private EventHandler updateDataHandler;
        GpsDeviceState device = null;
        GpsPosition position = null;
        private Label labelHeading;
        private TextBox textBoxHeading;
        private PictureBox pictureBoxCompassRose;
        private PictureBox pictureBoxPoint;
        private Timer timer;
        private MenuItem menuItemShowStatus;

        Gps gps = new Gps();

        public frmMain()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
            this.mainMenu = new System.Windows.Forms.MainMenu();
            this.exitMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItemGPS = new System.Windows.Forms.MenuItem();
            this.startGpsMenuItem = new System.Windows.Forms.MenuItem();
            this.stopGpsMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItemShowStatus = new System.Windows.Forms.MenuItem();
            this.status = new System.Windows.Forms.Label();
            this.labelHeading = new System.Windows.Forms.Label();
            this.textBoxHeading = new System.Windows.Forms.TextBox();
            this.pictureBoxCompassRose = new System.Windows.Forms.PictureBox();
            this.pictureBoxPoint = new System.Windows.Forms.PictureBox();
            this.timer = new System.Windows.Forms.Timer();
            this.SuspendLayout();
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.Add(this.exitMenuItem);
            this.mainMenu.MenuItems.Add(this.menuItemGPS);
            // 
            // exitMenuItem
            // 
            this.exitMenuItem.Text = "Exit";
            this.exitMenuItem.Click += new System.EventHandler(this.exitMenuItem_Click);
            // 
            // menuItemGPS
            // 
            this.menuItemGPS.MenuItems.Add(this.startGpsMenuItem);
            this.menuItemGPS.MenuItems.Add(this.stopGpsMenuItem);
            this.menuItemGPS.MenuItems.Add(this.menuItemShowStatus);
            this.menuItemGPS.Text = "GPS";
            // 
            // startGpsMenuItem
            // 
            this.startGpsMenuItem.Text = "Start GPS";
            this.startGpsMenuItem.Click += new System.EventHandler(this.startGpsMenuItem_Click);
            // 
            // stopGpsMenuItem
            // 
            this.stopGpsMenuItem.Enabled = false;
            this.stopGpsMenuItem.Text = "Stop GPS";
            this.stopGpsMenuItem.Click += new System.EventHandler(this.stopGpsMenuItem_Click);
            // 
            // menuItemShowStatus
            // 
            this.menuItemShowStatus.Text = "Show Status";
            this.menuItemShowStatus.Click += new System.EventHandler(this.menuItemShowStatus_Click);
            // 
            // status
            // 
            this.status.BackColor = System.Drawing.Color.White;
            this.status.Dock = System.Windows.Forms.DockStyle.Fill;
            this.status.Location = new System.Drawing.Point(0, 0);
            this.status.Name = "status";
            this.status.Size = new System.Drawing.Size(240, 268);
            this.status.Text = "status";
            this.status.Visible = false;
            // 
            // labelHeading
            // 
            this.labelHeading.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(184)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.labelHeading.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelHeading.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(93)))), ((int)(((byte)(4)))), ((int)(((byte)(2)))));
            this.labelHeading.Location = new System.Drawing.Point(3, 243);
            this.labelHeading.Name = "labelHeading";
            this.labelHeading.Size = new System.Drawing.Size(64, 20);
            this.labelHeading.Text = "Heading:";
            // 
            // textBoxHeading
            // 
            this.textBoxHeading.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.textBoxHeading.Location = new System.Drawing.Point(174, 243);
            this.textBoxHeading.Name = "textBoxHeading";
            this.textBoxHeading.ReadOnly = true;
            this.textBoxHeading.Size = new System.Drawing.Size(63, 21);
            this.textBoxHeading.TabIndex = 2;
            // 
            // pictureBoxCompassRose
            // 
            this.pictureBoxCompassRose.Dock = System.Windows.Forms.DockStyle.Top;
            this.pictureBoxCompassRose.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxCompassRose.Image")));
            this.pictureBoxCompassRose.Location = new System.Drawing.Point(0, 0);
            this.pictureBoxCompassRose.Name = "pictureBoxCompassRose";
            this.pictureBoxCompassRose.Size = new System.Drawing.Size(240, 240);
            // 
            // pictureBoxPoint
            // 
            this.pictureBoxPoint.BackColor = System.Drawing.Color.Black;
            this.pictureBoxPoint.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxPoint.Image")));
            this.pictureBoxPoint.Location = new System.Drawing.Point(115, 115);
            this.pictureBoxPoint.Name = "pictureBoxPoint";
            this.pictureBoxPoint.Size = new System.Drawing.Size(10, 10);
            // 
            // timer
            // 
            this.timer.Enabled = true;
            this.timer.Interval = 500;
            this.timer.Tick += new System.EventHandler(this.timer_Tick);
            // 
            // frmMain
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(184)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.textBoxHeading);
            this.Controls.Add(this.labelHeading);
            this.Controls.Add(this.pictureBoxPoint);
            this.Controls.Add(this.pictureBoxCompassRose);
            this.Controls.Add(this.status);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Menu = this.mainMenu;
            this.MinimizeBox = false;
            this.Name = "frmMain";
            this.Text = "GPS Compass";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Closed += new System.EventHandler(this.Form1_Closed);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        static void Main() 
        {
            Application.Run(new frmMain());
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }

            Close();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            updateDataHandler = new EventHandler(UpdateData);
         
            status.Text = "";
            
            status.Width = Screen.PrimaryScreen.WorkingArea.Width;
            status.Height = Screen.PrimaryScreen.WorkingArea.Height;

            gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
        }

        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            position = args.Position;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            Invoke(updateDataHandler);

        }

        void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
        {
            device = args.DeviceState;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            Invoke(updateDataHandler);
        }

        void UpdateData(object sender, System.EventArgs args)
        {
            if (gps.Opened)
            {
                string str = "";
                if (device != null)
                {
                    str = device.FriendlyName + " " + device.ServiceState + ", " + device.DeviceState + "\n";
                }

                if (position != null)
                {

                    if (position.LatitudeValid)
                    {
                        str += "Latitude (DD):\n   " + position.Latitude + "\n";
                        str += "Latitude (D,M,S):\n   " + position.LatitudeInDegreesMinutesSeconds + "\n";
                    }

                    if (position.LongitudeValid)
                    {
                        str += "Longitude (DD):\n   " + position.Longitude + "\n";
                        str += "Longitude (D,M,S):\n   " + position.LongitudeInDegreesMinutesSeconds + "\n";
                    }

                    if (position.SatellitesInSolutionValid &&
                        position.SatellitesInViewValid &&
                        position.SatelliteCountValid)
                    {
                        str += "Satellite Count:\n   " + position.GetSatellitesInSolution().Length + "/" +
                            position.GetSatellitesInView().Length + " (" +
                            position.SatelliteCount + ")\n";
                    }

                    if (position.TimeValid)
                    {
                        str += "Time:\n   " + position.Time.ToString() + "\n";
                    }

                    textBoxHeading.Text = position.Heading.ToString();

                    pictureBoxPoint.Left = (int)(116 + 100 * Math.Sin(position.Heading * Math.PI / 180));
                    pictureBoxPoint.Top = (int)(116 - 100 * Math.Cos(position.Heading * Math.PI / 180));
                }

                status.Text = str;

            }
        }

        private void Form1_Closed(object sender, System.EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }
        }

        private void stopGpsMenuItem_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }

            startGpsMenuItem.Enabled = true;
            stopGpsMenuItem.Enabled = false;
        }

        private void startGpsMenuItem_Click(object sender, EventArgs e)
        {
            if (!gps.Opened)
            {
                gps.Open();
            }

            startGpsMenuItem.Enabled = false;
            stopGpsMenuItem.Enabled = true;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            pictureBoxPoint.Visible = !pictureBoxPoint.Visible;
        }

        private void menuItemShowStatus_Click(object sender, EventArgs e)
        {
            menuItemShowStatus.Checked = !menuItemShowStatus.Checked;

            timer.Enabled = !menuItemShowStatus.Checked;

            labelHeading.Visible = !menuItemShowStatus.Checked;
            textBoxHeading.Visible = !menuItemShowStatus.Checked;

            pictureBoxCompassRose.Visible = !menuItemShowStatus.Checked;
            pictureBoxPoint.Visible = !menuItemShowStatus.Checked;

            status.Visible = menuItemShowStatus.Checked;
        }
    }
}
